package com.lincoln.system.app.config;

import com.lincoln.entity.client.DbClient;
import com.lincoln.entity.permission.DbPermission;
import com.lincoln.entity.permission.DbUserPermission;
import com.lincoln.entity.role.DbRole;
import com.lincoln.entity.user.DbUser;
import com.lincoln.service.PermissionService;
import com.lincoln.service.RoleService;
import com.lincoln.service.TokenClientService;
import com.lincoln.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.Order;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * 启动服务器前执行
 * Date:2017/8/21.
 * Time:15:36
 */
@Component
@Service
@Order(value = 1)
public class StartRun implements CommandLineRunner, ApplicationListener<ContextRefreshedEvent> {
    private static ApplicationContext applicationContext = null;
    @Autowired
    PermissionService permissionService;
    @Autowired
    RoleService roleService;
    @Autowired
    TokenClientService clientService;
    @Autowired
    UserService userService;
    @Autowired
    DataSource dataSource;

    @Override
    public void run(String... args) throws Exception {
        if (userService.findAll().size() <= 0) {
            //在这里添加默认数据
            DbPermission permission = new DbPermission();
            permission.setName("default_permission");
            permission = permissionService.save(permission);

            DbRole role = new DbRole();
            role.setName("role_admin");
            List<DbPermission> list = new ArrayList<>();
            list.add(permission);
            role.setPermissions(list);
            role = roleService.save(role);

            DbClient client = new DbClient();
            client.setClientId("system-app");
            client.setResourceIds("system-app");
            client.setAuthorizedGrantTypes("authorization_code,password,refresh_token,implicit");
            client.setAccessTokenValidity(900);
            client.setRefreshTokenValidity(3600);
            client.setClientSecret(PasswordEncoderFactories.createDelegatingPasswordEncoder().encode("systemapp"));
            client.setScope("role_admin");
            client = clientService.save(client);

            DbUser user = new DbUser();
            user.setRole(role);
            user.setUserName("admin");
            user.setPassword(PasswordEncoderFactories.createDelegatingPasswordEncoder().encode("123456"));
            user.setAccountNonLocked(true);
            user.setEnabled(true);
            user.setCredentialsNonExpired(true);
            user.setAccountNonExpired(true);
            userService.save(user);
        }
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (applicationContext == null) {
            applicationContext = contextRefreshedEvent.getApplicationContext();
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
